package cn.bdqn.pn.service.commodity.CommodityServiceImpl;

import cn.bdqn.pn.pojo.commodity.Showtype;
import cn.bdqn.pn.reposity.commodity.ShowTypeMapper;

import cn.bdqn.pn.service.commodity.ShowTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service("showTypeService")
public class ShowTypeServiceImpl implements ShowTypeService {
    @Autowired
    private  ShowTypeMapper showTypeMapper;



    @Override
    public Showtype checkRepeat(Showtype showtype) {
        Specification<Showtype> specification=new Specification<Showtype>() {
            @Override
            public Predicate toPredicate(Root<Showtype> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list=new ArrayList<>();
                Predicate[] predicates=new Predicate[list.size()];
                list.add(criteriaBuilder.equal(root.get("typeName"),showtype.getTypeName()));
                System.out.println("检查重复");
                return criteriaBuilder.and(list.toArray(predicates));
            }
        };
        return showTypeMapper.findOne(specification);
    }


    @Override
    public Page<Showtype> findshowTypeNoCriteria( Integer pageNo,Integer pageSize,Showtype showtype) {
        Specification<Showtype> specification=new Specification<Showtype>() {
            @Override
            public Predicate toPredicate(Root<Showtype> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                Predicate[] predicate =new Predicate[list.size()];
                list.add(cb.equal(root.get("fatherId"),showtype.getFatherId()));
                return cb.and(list.toArray(predicate));
            }
        };
        Pageable pageable=new PageRequest(pageNo-1,pageSize);
        return showTypeMapper.findAll(specification,pageable);
    }

    @Override
    public List<Showtype> listShowTypeByFatherId(Showtype showtype) throws Exception {
        Specification<Showtype> specification=new Specification<Showtype>() {
            @Override
            public Predicate toPredicate(Root<Showtype> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                Predicate[] predicate =new Predicate[list.size()];
                System.out.println("------------查询全部父类，不分页-----------");
                list.add(cb.equal(root.get("fatherId"),showtype.getFatherId()));
                return cb.and(list.toArray(predicate));
            }
        };
        return showTypeMapper.findAll(specification);
    }



    @Override
    public Showtype addshowType(Showtype showtype) {
        return showTypeMapper.save(showtype) ;
    }

    @Override
    public void edit(Showtype showtype) {
        showTypeMapper.save(showtype);
    }

    @Override
    public void delete(Integer showTypeId) {
        showTypeMapper.delete(showTypeId);
    }

    @Override
    public Page<Showtype> findshowTypeNoCriteria( Integer page, Integer size) {
        Specification<Showtype> specification=new Specification<Showtype>() {
            @Override
            public Predicate toPredicate(Root<Showtype> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();


                Predicate[] p =new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        };

        Pageable pageable=new PageRequest(page=page==null?0:page,size=size==null?5:size,new Sort(new Sort.Order(Sort.Direction.DESC,"showTypeId")));
        return showTypeMapper.findAll(specification,pageable);
    }



    @Override
    public List<Showtype> ShowTypelist() {
        return showTypeMapper.findAll();
    }

    @Override
    public List<Showtype> getAllfatherId(Integer fatherId) {
        return showTypeMapper.findShowtypesByFatherId(fatherId);
    }
    @Override
    public Showtype findShowTypeById(Integer showTypeId) {
        return showTypeMapper.findOne(showTypeId);
    }


}



